<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
	<head>
		<th:block th:replace="tpl :: common_head()"></th:block>
		<title>用户编辑</title>
	</head>
	<body th:object="${obj}" th:with="entrance='/system/user/index'">
		<th:block th:replace="tpl :: common_body(~{::.main-right})">
		<div class="main-right">
			<h1 class="main-right-h1">用户管理</h1>
			<ul class="main-right-tab">
				<li><a href="index">用户列表</a></li>
			    <li class="selected" th:if="*{id==null}">新增用户</li>
			    <li class="selected" th:if="*{id!=null}">编辑用户</li>
			</ul>
			<form class="form" method="POST">
<fieldset>
	<legend>用户信息</legend>
	<dl>
		<dt>帐号</dt>
		<dd>
			<input type="text" th:field="*{loginName}" placeholder="请输入帐号，至少4个字符"
				th:disabled="*{id!=null}" data-verify="required(4)|username" />
		</dd>
		<dt>姓名</dt>
		<dd>
			<input type="text" th:field="*{name}" placeholder="姓名至少2个字符" data-verify="required(2)" />
		</dd>
		<th:block th:if="*{id==null}">
		<dt>密码</dt>
		<dd>
			<input type="password" th:field="*{password}" placeholder="请输入5至15位密码" data-verify="required(5,15)" />
		</dd>
		<dt>确认密码</dt>
		<dd>
			<input type="password" placeholder="重新输入密码" data-verify="required|pass2" id="password2" />
		</dd>
		</th:block>
		<dt>手机号码</dt>
		<dd>
			<input type="text" th:field="*{mobile}" data-verify="phone" />
		</dd>
		<dt>邮箱</dt>
		<dd>
			<input type="text" th:field="*{email}" data-verify="email" />
		</dd>
		<dt>可登录</dt>
		<dd>
			<input type="checkbox" id="enabled" name="enabled" value="true" th:checked="*{enabled}" />
		    <label for="enabled">可登录</label>
		</dd>
		<dt>用户角色</dt>
		<dd>
			<th:block th:each="role,roleStat:${roles}">
				<input type="checkbox" name="role" th:value="${role.id}" 
		    		th:checked="${userRoleIds.indexOf(role.id) gt -1}"
		    		th:id="'roles['+${roleStat.count-1}+'].id'" />
		    	<label th:for="'roles['+${roleStat.count-1}+'].id'" th:text="${role.name}"></label>
			</th:block>
		</dd>
	</dl>
</fieldset>
<blockquote class="blockquote">
	提示：用户实际拥有的权限包括角色权限与用户权限，若角色已拥有权限，则不需要重复授权
</blockquote>
<fieldset>
	<legend>权限列表</legend>
	<dl>
		<th:block th:each="domain : ${domains}">
			<dt th:text="${domain}"></dt>
			<dd>
				<th:block th:each="permission : ${permissions}" th:if="${permission.domain?.equals(domain)}" th:with="seq=${T(Integer).valueOf(#ids.seq(''))-1}">
				<input type="checkbox" th:id="'permissions['+${seq}+'].id'"
						name="permission" th:value="${permission.id}"
						th:checked="${userPermissionIds.indexOf(permission.id) gt -1}" >
				<label th:text="${permission.name}" th:for="'permissions['+${seq}+'].id'"></label>
				</th:block>
			</dd>
		</th:block>
	</dl>
</fieldset>
<input type="submit" value="保存修改" />
<input type="button" class="btn-delete" value="删&emsp;&emsp;除" th:if="*{id != null}" />
			</form>
		</div>
		</th:block>
		<script>
			$(function(){
				$.verify({
					"username":function(){
						var username=$("#loginName").val();
						return $.get(location.href+"/checkUsername",{username:username});
					},
					"pass2":function(){
						var thisVal=$(this).val();
						return $.Deferred(function(deferred){
							var pass=$("#password").val();
							if(pass!=thisVal){
								deferred.reject({errMsg:"两次输入密码不一致"})
								return;
							}
							deferred.resolve();
						}).promise();
					}
				})
			});
		</script>
	</body>
</html>